﻿Imports System.Data.OleDb
Imports System.IO
Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TwilightConn").ConnectionString)
            Dim SqlString As String = "SELECT * FROM Profile WHERE TwiWriter=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            'Need to get a record back so establish a data reader
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("TwilighterName")) Then tb_profileName.Text = dr("TwilighterName")
                If Not IsDBNull(dr("Gender")) Then rbl_profileGender.SelectedValue = dr("Gender")
                If Not IsDBNull(dr("Team")) Then ddl_profileTeam.SelectedValue = dr("Team")
                If Not IsDBNull(dr("AboutMe")) Then tb_profileAboutMe.Text = dr("AboutMe")
            End If
        End If
    End Sub
    Protected Sub btn_saveProfile_Click(sender As Object, e As EventArgs) Handles btn_saveProfile.Click
        Dim SqlString As String = ""
        Dim newFileName As String = ""
        If fu_profileAvatar.HasFile Then
            Dim myGUID = Guid.NewGuid()
            Dim fileExtension As String
            fileExtension = System.IO.Path.GetExtension(fu_profileAvatar.FileName).ToLower()
            If fileExtension = ".jpg" Then
                newFileName = myGUID.ToString() & ".jpg"
            ElseIf fileExtension = ".png" Then
                newFileName = myGUID.ToString() & ".png"
            ElseIf fileExtension = ".gif" Then
                newFileName = myGUID.ToString() & ".gif"
            End If
            Dim fileLocationOnServerHardDisk = Request.MapPath("~/Pictures") & "/" & newFileName
            fu_profileAvatar.SaveAs(fileLocationOnServerHardDisk)
        End If

        Dim newFileName2 As String = ""
        If fu_profilePagePicture.HasFile Then
            Dim myGUID2 = Guid.NewGuid()
            Dim fileExtension2 As String
            fileExtension2 = System.IO.Path.GetExtension(fu_profilePagePicture.FileName).ToLower()
            If fileExtension2 = ".jpg" Then
                newFileName2 = myGUID2.ToString() & ".jpg"
            ElseIf fileExtension2 = ".png" Then
                newFileName2 = myGUID2.ToString() & ".png"
            ElseIf fileExtension2 = ".gif" Then
                newFileName2 = myGUID2.ToString() & ".gif"
            End If
            Dim fileLocationOnServerHardDisk2 = Request.MapPath("~/Pictures") & "/" & newFileName2
            fu_profilePagePicture.SaveAs(fileLocationOnServerHardDisk2)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TwilightConn").ConnectionString)

        If fu_profilePagePicture.HasFile And (fu_profileAvatar.HasFile Or (rbl_profileAvatars.SelectedValue = "1" Or rbl_profileAvatars.SelectedValue = "2" Or rbl_profileAvatars.SelectedValue = "3" Or rbl_profileAvatars.SelectedValue = "4" Or rbl_profileAvatars.SelectedValue = "5" Or rbl_profileAvatars.SelectedValue = "6")) Then
            SqlString = "UPDATE Profile SET TwilighterName=@f1,Avatar=@f2,Gender=@f3,Team=@f4,MyPicture=@f5,AboutMe=@f6 WHERE TwiWriter=@f7"
        ElseIf fu_profilePagePicture.HasFile And (fu_profileAvatar.HasFile = False Or rbl_profileAvatars.SelectedValue = "") Then
            SqlString = "UPDATE Profile SET TwilighterName=@f1,Gender=@f3,Team=@f4,MyPicture=@f5,AboutMe=@f6 WHERE TwiWriter=@f7"
        ElseIf fu_profilePagePicture.HasFile = False And (fu_profileAvatar.HasFile Or (rbl_profileAvatars.SelectedValue = "1" Or rbl_profileAvatars.SelectedValue = "2" Or rbl_profileAvatars.SelectedValue = "3" Or rbl_profileAvatars.SelectedValue = "4" Or rbl_profileAvatars.SelectedValue = "5" Or rbl_profileAvatars.SelectedValue = "6")) Then
            SqlString = "UPDATE Profile SET TwilighterName=@f1,Avatar=@f2,Gender=@f3,Team=@f4,AboutMe=@f6 WHERE TwiWriter=@f7"
        Else
            SqlString = "UPDATE Profile SET TwilighterName=@f1,Gender=@f3,Team=@f4,AboutMe=@f6 WHERE TwiWriter=@f7"
        End If
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        oleDbConn.Open()

        cmd.Parameters.AddWithValue("@f1", tb_profileName.Text)

        'If user chooses their own picture we'll set it to that, otherwise set to their chosen default. If they don't select ANY then a default 'avatar1' is given
        If fu_profileAvatar.HasFile Then
            cmd.Parameters.AddWithValue("@f2", "Pictures/" & newFileName)
        ElseIf rbl_profileAvatars.SelectedValue = "1" Then
            cmd.Parameters.AddWithValue("@f2", "Pictures/Design/avatar1.jpg")
        ElseIf rbl_profileAvatars.SelectedValue = "2" Then
            cmd.Parameters.AddWithValue("@f2", "Pictures/Design/avatar2.jpg")
        ElseIf rbl_profileAvatars.SelectedValue = "3" Then
            cmd.Parameters.AddWithValue("@f2", "Pictures/Design/avatar3.gif")
        ElseIf rbl_profileAvatars.SelectedValue = "4" Then
            cmd.Parameters.AddWithValue("@f2", "Pictures/Design/avatar4.gif")
        ElseIf rbl_profileAvatars.SelectedValue = "5" Then
            cmd.Parameters.AddWithValue("@f2", "Pictures/Design/avatar5.jpg")
        ElseIf rbl_profileAvatars.SelectedValue = "6" Then
            cmd.Parameters.AddWithValue("@f2", "Pictures/Design/avatar6.png")
        Else
            cmd.Parameters.AddWithValue("@f2", "Pictures/Design/avatar1.jpg")
        End If

        cmd.Parameters.AddWithValue("@f3", rbl_profileGender.SelectedValue)
        cmd.Parameters.AddWithValue("@f4", ddl_profileTeam.SelectedValue)
        If fu_profilePagePicture.HasFile Then cmd.Parameters.AddWithValue("@f5", "Pictures/" & newFileName2)
        cmd.Parameters.AddWithValue("@f6", tb_profileAboutMe.Text)
        cmd.Parameters.AddWithValue("@f7", User.Identity.Name)

        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("Profile.aspx?TwiWriter=" & User.Identity.Name)

    End Sub
End Class